<?php declare(strict_types=1);

namespace App\Services\Easypay\Incoming;


use App\Models\EasypayConfig;
use App\Models\EasypayStore;
use App\Models\Store;
use Illuminate\Support\Facades\Log;

class Config
{

    const DOAMIN_PRO = "https://mms.eycard.cn";

    const DOMAIN_DEV = "https://mtest.eycard.cn:4443";

    const APPLAY = '/AG_MerchantManagementSystem_Core/agent/api/gen';

    const IMG_UPDATE = '/AG_MerchantManagementSystem_Core/agent/api/imgUpl';

    /**
     * 联合收单V2
     */
    const DOAMIN_PROV2 = "https://phoenix.eycard.cn";

    const DOMAIN_DEVV2 = "https://d-phoenix-gap.easypay.com.cn:24443";

    const APPLY_QUERY = '/yqt/merchant/marketing/detailQuery';

    const ACT_QUERY = '/yqt/merchant/marketing/query';


    /**
     * 电子签名
     */
    const channelId = "";//电子协议系统分配的机构号

    const merId = "";//电子协议系统分配的商户号

    const termId = "";//电子协议系统分配的终端号


    const ContractServet = "/EasypayMPSystem/ContractServet";


    public static function getDomain()
    {
        if (env("APP_ENV") == "dev") {
            $domain = self::DOMAIN_DEV;
        } else {
            $domain = self::DOAMIN_PRO;
        }

        return $domain;
    }


    public static function getDomainV2()
    {
        if (env("APP_ENV") == "dev") {
            $domain = self::DOMAIN_DEVV2;
        } else {
            $domain = self::DOAMIN_PROV2;
        }

        return $domain;
    }


    public static function sign(EasypayConfig $config, array $params): string
    {
        ksort($params);
        $stringToBeSigned = "";
        foreach ($params as $k => $v) {
            if (is_array($v)) {
                $v = json_encode($v);
            }
            $stringToBeSigned .= $k . "=" . $v . "&";
        }
        $stringToBeSigned = rtrim($stringToBeSigned, "&");

        $sign = strtoupper(md5($stringToBeSigned . '&key=' . $config->key));
        Log::channel("easypay")->info("sign-string", [
            "params" => $params,
            "str" => $stringToBeSigned . '&key=' . $config->key,
            "sign" => $sign
        ]);
        return $sign;
    }


    public static function curl(string $url, array $params, array $headers)
    {
        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            $params = http_build_query($params);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            if (!empty($headers)) {
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            }
            $result = curl_exec($ch);
            if (curl_errno($ch)) {
                throw new \Exception(curl_error($ch));
            }
            curl_close($ch);
            return $result;
        } catch (\Exception $ex) {
            throw new \Exception($ex->getMessage());
        }
    }


    public function v2sign($reqHeader, $params, $priKey)
    {
        ksort($params);
        ksort($reqHeader);
        $str = json_encode($reqHeader) . strtoupper(md5(json_encode($params)));
        $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
            wordwrap($priKey, 64, "\n", true) .
            "\n-----END RSA PRIVATE KEY-----";

        ($res) or die('您使用的私钥格式错误，请检查RSA私钥配置');

        openssl_sign($str, $sign, $res, OPENSSL_ALGO_SHA256);

        $sign = base64_encode($sign);
        return $sign;

    }


}